home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / IBPalettes / WW3DKit / spline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-22  |  1.3 KB  |  53 lines

  1. #include "proctext.h"
  2.  
  3. /* Coefficients of basis matrix. */
  4. #define CR00     -0.5
  5. #define CR01      1.5
  6. #define CR02     -1.5
  7. #define CR03      0.5
  8. #define CR10      1.0
  9. #define CR11     -2.5
  10. #define CR12      2.0
  11. #define CR13     -0.5
  12. #define CR20     -0.5
  13. #define CR21      0.0
  14. #define CR22      0.5
  15. #define CR23      0.0
  16. #define CR30      0.0
  17. #define CR31      1.0
  18. #define CR32      0.0
  19. #define CR33      0.0
  20.  
  21. float
  22. spline(float x, int nknots, float *knot)
  23. {
  24.     int span;
  25.     int nspans = nknots - 3;
  26.     float c0, c1, c2, c3;    /* coefficients of the cubic.*/
  27.  
  28.     if (nspans < 1) {  /* illegal */
  29.         fprintf(stderr, "Spline has too few knots.\n");
  30.         return 0;
  31.     }
  32.  
  33.     /* Find the appropriate 4-point span of the spline. */
  34.     x = clamp(x, 0, 1) * nspans;
  35.     span = (int) x;
  36.     if (span >= nknots - 3)
  37.         span = nknots - 3;
  38.     x -= span;
  39.     knot += span;
  40.  
  41.     /* Evaluate the span cubic at x using Horner's rule. */
  42.     c3 = CR00*knot[0] + CR01*knot[1]
  43.        + CR02*knot[2] + CR03*knot[3];
  44.     c2 = CR10*knot[0] + CR11*knot[1]
  45.        + CR12*knot[2] + CR13*knot[3];
  46.     c1 = CR20*knot[0] + CR21*knot[1]
  47.        + CR22*knot[2] + CR23*knot[3];
  48.     c0 = CR30*knot[0] + CR31*knot[1]
  49.        + CR32*knot[2] + CR33*knot[3];
  50.  
  51.     return ((c3*x + c2)*x + c1)*x + c0;
  52. }
  53.